﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity.Model;
using SystemFrameWork;
using SystemFrameWork.Enums;

namespace DutyWeb.Admin
{
    public partial class Sys_UserGroupList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.PaginationBarUserGroup.PageIndexChanged += new EventHandler(this.Paging);
            //if (!Page.IsPostBack)
            //{
            //    this.BindQuestions(ConstantsManager.FirstPage);
            //    this.PaginationBarUserGroup.PageIndex = ConstantsManager.FirstPage;
            //    this.PaginationBarUserGroup.PageSize = this.PageSize;
            //    this.PaginationBarUserGroup.AllCount = base.CurrentTotalRecord;
            //    this.PaginationBarUserGroup.DataBind();
            //}
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.PaginationBarUserGroup.PageSize = this.PageSize;
            this.bindUserGroup(ConstantsManager.FirstPage);
            this.PaginationBarUserGroup.PageIndex = ConstantsManager.FirstPage;

            this.PaginationBarUserGroup.AllCount = base.CurrentTotalRecord;
            this.PaginationBarUserGroup.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        private void bindUserGroup(int pageIndex)
        {
            RoleView uGroupView = new RoleView();
            uGroupView.RoleName = this.txtUserGroupName.Text.Trim();
            uGroupView.DepartmentName = this.DepartmentTree1.SelectedDepartmentName;
            uGroupView.MyPageModel = new PageModel();
            uGroupView.MyPageModel.PageIndex = pageIndex;
            uGroupView.MyPageModel.PageSize = this.PageSize;
            uGroupView.OrderByField = "ModifiedOn";
            uGroupView.OrderDirection = "desc";
            Guid? departmentID = this.DepartmentTree1.SelectedDepartmentID;
            //this.DepartmentTree1.SelectedDepartmentID = null;
            List<RoleView> list = base.sys_RoleManagement.LoadUserGroupByCondition(uGroupView);
            this.gvUserGroup.DataSource = list;
            this.gvUserGroup.DataBind();
            base.CurrentTotalRecord = uGroupView.MyPageModel.TotalRecord;
            //this.DepartmentTree1.LoadDepartmentTree();
        }

        /// <summary>
        /// bind the question data 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Paging(object sender, EventArgs e)
        {
            this.bindUserGroup(PaginationBarUserGroup.PageIndex);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="functionModule_Group"></param>
        public void Insert(Sys_FunctionModuleSys_Role functionModule_Group)
        {

        }

        protected void gvUserGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }
        protected void gvUserGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                DeleteUserGroups(null);
                ////bind the paging control
                this.PaginationBarUserGroup.PageIndex = ConstantsManager.FirstPage;
                this.PaginationBarUserGroup.PageSize = this.PageSize;
                this.PaginationBarUserGroup.AllCount = base.CurrentTotalRecord;
                this.PaginationBarUserGroup.DataBind();
            }
            catch
            {
                ShowMessage("操作出现异常，请联系管理员!", ExceptionLevel.Info);
                return;
            }
        }

        /// <summary>
        /// remove the selected questions
        /// </summary>
        /// <param name="e"></param>
        private void DeleteUserGroups(GridViewCommandEventArgs e)
        {
            if (base.CurrentUserID == Guid.Empty)
            {
                base.ShowMessage("请登录后再操作！", ExceptionLevel.Info);
                return;
            }

            List<Sys_Role> lstUserGroup = new List<Sys_Role>();

            if (e != null)
            {
                GridViewRow row = ((e.CommandSource as LinkButton).NamingContainer) as GridViewRow;
                int rowIndex = row.RowIndex;
                Guid id = new Guid(this.gvUserGroup.DataKeys[rowIndex].Value.ToString());
                Sys_Role ug = new Sys_Role();
                ug.ID = id;
                lstUserGroup.Add(ug);
            }
            else
            {
                int flag = 0;
                foreach (GridViewRow myGridViewRow in this.gvUserGroup.Rows)
                {
                    CheckBox myCheckBox = myGridViewRow.FindControl(ConstantsManager.chkSingleKey) as CheckBox;

                    if (myCheckBox.Checked)
                    {
                        flag++;
                        Sys_Role ug = new Sys_Role();
                        ug.ID = new Guid(this.gvUserGroup.DataKeys[myGridViewRow.DataItemIndex].Value.ToString());
                        ug.CurrentUserID = base.CurrentUserID;
                        lstUserGroup.Add(ug);
                    }
                }


                if (flag == 0)
                {
                    base.ShowMessage("请选择删除项！", ExceptionLevel.Info);
                    return;
                }
            }


            base.sys_RoleManagement.BulkRemoveTheSys_Role(lstUserGroup);

            this.bindUserGroup(ConstantsManager.FirstPage);
        }

    }
}
